part of '../widgets.dart';

class VanCheckboxGroup extends StatelessWidget with VanProvide {
  /// props - 所有选中项的标识符
  late final Ref<List> controller;

  /// props - 是否禁用所有复选框(默认: false)
  final bool disabled;

  /// props - 排列方向，可选值为 horizontal(默认: vertical)
  final VanCheckboxDirection direction;

  /// props - 所有复选框的图标大小，默认单位为 px(默认: 20px)
  final double? iconSize;

  /// props - 所有复选框的选中状态颜色(默认: #1989fa)
  final Color? checkedColor;

  /// props - 形状，可选值为 square(默认: round)
  final VanCheckboxShape shape;

  /// events - 当绑定值变化时触发的事件
  final Function(List<dynamic> value)? onChange;

  /// slots - 默认插槽 - 单组件 转 多组件
  /// <br>type: [String] or [Widget]
  final dynamic child;

  /// slots - 默认插槽 - 多子组件
  final List<Widget>? children;

  /// props - 最大可选数，0 为无限制(默认: 0)
  int get max => controller is VanCheckboxGroupController ? (controller as VanCheckboxGroupController).max : 0;

  /// props - 选中值[列表]
  get value => controller.value;

  VanCheckboxGroup({
    super.key,
    List<dynamic>? value,
    Ref<List>? controller,
    this.disabled = false,
    int? max,
    this.direction = VanCheckboxDirection.vertical,
    this.iconSize,
    this.checkedColor,
    this.shape = VanCheckboxShape.square,
    this.onChange,
    this.child,
    this.children,
  }) : controller = controller ?? VanCheckboxGroupController(value ?? const [], max: max ?? 0);

  @override
  Widget build(BuildContext context) {
    if (onChange != null) controller.addListener(() => onChange!(controller.value));

    List<Widget> ws;
    Widget? widget;

    if (child is String) {
      widget = Text(child);
    } else if (child is Widget) {
      widget = child;
    } else if (child != null) {
      _logger.e("VanCheckboxGroup.child only type support [String] or [Widget]");
    }

    if (widget != null) {
      ws = [widget, ...children ?? []];
    } else {
      ws = children ?? [];
    }

    if (direction == VanCheckboxDirection.vertical) {
      widget = Column(children: ws);
    } else {
      widget = Row(children: ws);
    }
    return VanProvider(provide: this, child: widget);
  }
}

enum VanCheckboxShape { round, square }

enum VanCheckboxPosition { left, right }

enum VanCheckboxDirection { horizontal, vertical }

/// 切换所有复选框，传 true 为选中，false 为取消选中，不传参为取反
class VanCheckboxGroupController extends VanCtrl<List> {
  final int max;
  final Set<_VanCheckboxState> _states = HashSet();

  get checkedLength => value.length;

  VanCheckboxGroupController(super.value, {this.max = 0});

  /// 切换所有复选框，传 true 为选中，false 为取消选中，不传参为取反
  void toggleAll({bool? checked, bool? skipDisabled}) {
    Set values = HashSet();
    for (var d in _states) {
      if (!d.widget.bindGroup) continue;
      if (skipDisabled == true && d.widget.disabled == true) continue;
      bool? checked0 = checked;
      if (max > 0 && values.length > max) return;
      checked0 ??= !value.contains(d.widget.value);
      if (checked0) {
        values.add(d.widget.value);
      } else {
        values.remove(d.widget.value);
      }
    }
    value = values.toList(growable: false);
  }

  set toggleValue(value) {
    final values = [...this.value];
    final success = values.remove(value);
    if (!success) values.add(value);
    this.value = values;
  }
}
